package adminSys

import (
	adminSys "fiberAmisAdmin/controllers/adminSys/sysServer"
	"fiberAmisAdmin/controllers/amis"
	"fmt"
	"github.com/gofiber/fiber/v2"
)

type MenuController struct {
	BaseSysController
}

func (uc *MenuController) Config() map[string]interface{} {
	return map[string]interface{}{
		"tableName": "admin_menus",
		"page":      "1",
		"pageSize":  "15",
	}
}

// BuildConditions 构建查询条件的方法
func (uc *MenuController) BuildWhere() (string, []interface{}) {
	conditions := "parent_id = ?"
	params := []interface{}{0}
	return conditions, params
}

// buildAttribute 构建属性
func (uc *MenuController) BuildAttribute(page string, pageSize string) map[string]interface{} {
	return map[string]interface{}{
		"tableName": uc.Config()["tableName"],
		"page":      page,
		"pageSize":  pageSize,
	}
}

// List 方法用于处理获取用户列表请求
func (uc *MenuController) List(c *fiber.Ctx) error {
	page := c.Query("page", uc.Config()["page"].(string))
	pageSize := c.Query("perPage", uc.Config()["pageSize"].(string))
	attribute := uc.BuildAttribute(page, pageSize)

	fields := []map[string]interface{}{
		{
			"name":  "id",
			"label": "ID",
			"type":  "text",
		}, {
			"label":    "名称",
			"name":     "title",
			"type":     "text",
			"sortable": true,
			"filterable": map[string]interface{}{
				"options": []map[string]interface{}{
					{
						"label": "0",
						"value": 0,
					},
					{
						"label": "1",
						"value": 1,
					},
				},
			},
		},
		//{
		//	"label":      "方法",
		//	"name":       "method",
		//	"type":       "text",
		//	"quickEdit":  true,
		//	"sortable":   true,
		//	"searchable": true,
		//},

		{
			"label": "图标",
			"name":  "icon",
			"type":  "text",
		}, {
			"label": "时间",
			"name":  "updated_at",
			"type":  "text",
		},
	}

	button := map[string]bool{
		"showCreateButton": true,
		"showEditButton":   true,
		"showDeleteButton": true,
		"showDetailButton": true,
	}
	adminServer := &adminSys.AdminSysMenu{
		MenuServer: &adminSys.MenuServer{},
	}
	conditions, params := uc.BuildWhere()
	data, err := amis.TableColumn(fields, uc.Form(), uc.detail(), c.Path(), attribute, button, conditions, params, adminServer.MenuServer)
	if err != nil {
		fmt.Println("err:", err)
		return uc.returnError(c, fiber.StatusOK, "Error")
	}
	return uc.returnSuccess(c, fiber.StatusOK, "", data)
}

// form 方法定义表单字段
func (uc *MenuController) Form() []map[string]interface{} {
	fields := []map[string]interface{}{
		{
			"label":       "用户 ID",
			"name":        "user_id",
			"type":        "input-text",
			"required":    true,
			"placeholder": "请输入用户 ID",
		},
		{
			"label": "方法",
			"name":  "method",
			"type":  "input-text",
		},
		{
			"label": "路径",
			"name":  "path",
			"type":  "input-text",
		},
		{
			"label": "IP",
			"name":  "ip",
			"type":  "input-text",
		},
		{
			"label": "input",
			"name":  "input",
			"type":  "input-text",
		},
	}
	return fields
}

// detail 方法定义详细信息字段
func (uc *MenuController) detail() []map[string]interface{} {
	fields := []map[string]interface{}{
		{
			"label": "用户 ID",
			"name":  "user_id",
			"type":  "text",
		},
		{
			"label": "方法",
			"name":  "method",
			"type":  "text",
		},
		{
			"label": "路径",
			"name":  "path",
			"type":  "text",
		}, {
			"label": "时间",
			"name":  "updated_at",
			"type":  "text",
		},
	}
	return fields
}
